from pydantic import BaseModel, Field
from datetime import datetime
from typing import List, Optional, Dict, Any


class OperationLogInfo(BaseModel):
    """操作日志信息模型"""
    id: int = Field(description="日志ID")
    user_id: int = Field(description="操作用户ID")
    username: str = Field(description="操作用户名")
    module: str = Field(description="操作模块")
    action: str = Field(description="操作动作")
    method: str = Field(description="HTTP方法")
    path: str = Field(description="请求路径")
    ip: str = Field(description="IP地址")
    user_agent: Optional[str] = Field(None, description="用户代理")
    request_data: Optional[Dict[str, Any]] = Field(None, description="请求数据")
    response_data: Optional[Dict[str, Any]] = Field(None, description="响应数据")
    status_code: int = Field(description="状态码")
    duration: float = Field(description="执行时长(秒)")
    success: bool = Field(description="是否成功")
    error_message: Optional[str] = Field(None, description="错误信息")
    created_at: datetime = Field(description="创建时间")

    class Config:
        from_attributes = True


class OperationLogListResponse(BaseModel):
    """操作日志列表响应模型"""
    total: int = Field(description="总数")
    items: List[OperationLogInfo] = Field(description="日志列表")


class LogQueryParams(BaseModel):
    """日志查询参数模型"""
    username: Optional[str] = Field(None, description="用户名")
    module: Optional[str] = Field(None, description="操作模块")
    action: Optional[str] = Field(None, description="操作动作")
    method: Optional[str] = Field(None, description="HTTP方法")
    ip: Optional[str] = Field(None, description="IP地址")
    success: Optional[bool] = Field(None, description="是否成功")
    status_code: Optional[int] = Field(None, description="状态码")
    start_time: Optional[datetime] = Field(None, description="开始时间")
    end_time: Optional[datetime] = Field(None, description="结束时间")


class LogStatistics(BaseModel):
    """日志统计模型"""
    total_logs: int = Field(description="总日志数")
    success_logs: int = Field(description="成功日志数")
    error_logs: int = Field(description="错误日志数")
    today_logs: int = Field(description="今日日志数")
    avg_duration: float = Field(description="平均执行时长")
    top_modules: List[Dict[str, Any]] = Field(description="热门模块")
    top_users: List[Dict[str, Any]] = Field(description="活跃用户")


class LogExportParams(BaseModel):
    """日志导出参数模型"""
    format: str = Field("excel", description="导出格式：excel, csv, json")
    start_time: Optional[datetime] = Field(None, description="开始时间")
    end_time: Optional[datetime] = Field(None, description="结束时间")
    modules: Optional[List[str]] = Field(None, description="指定模块")
    success_only: bool = Field(False, description="仅导出成功日志") 